import { ElMessage } from "element-plus";
import "element-plus/dist/index.css";
import CryptoJS from "crypto-js";
const secretKey = "wangpeng"; // 用一个密钥来加密解密
//修改浏览器图标
export const updateFavicon = (iconUrl) => {
  let link = document.querySelector("link[rel*='icon']"); // 获取当前的favicon标签
  if (link) {
    link.href = iconUrl; // 修改favicon的href属性
  } else {
    // 如果没有找到favicon标签，动态创建一个新的
    link = document.createElement("link");
    link.rel = "icon";
    link.type = "image/svg+xml";
    link.href = iconUrl;
    document.head.appendChild(link); // 将新的favicon标签添加到<head>中
  }
};
//信息提示
export const updateMessage = (message, type = "error", duration = 1000) => {
  ElMessage({
    message: message,
    type: type,
    duration: duration,
  });
};
export const propWidth = (prop) => {
  switch (prop) {
    case "id":
      return "60";
    case "phone":
    case "name":
      return "120";
    case "remark":
    case "time_limit":
      return "300";
    case "schedule":
      return "430";
    default:
      return "100";
  }
};
//获取阿里巴巴icon图标
export const getIcon = (icon) => {
  return `custom-icon custom-icon-${icon}`;
};
//数组存取值
export const updateOrGetValue = (
  findName,
  keyName,
  array,
  value = undefined,
  key = "id"
) => {
  // 查找对应的 item，使用动态的 key 属性
  const item = array.find((item) => item[key] === findName);

  // 如果找到了 item
  if (item) {
    if (value !== undefined) {
      // 如果传入了 value，设置值
      item[keyName] = value;
    } else {
      // 如果没有传入 value，返回值
      return item[keyName];
    }
  }

  // 如果没有找到 item 或者没有设置值，返回 undefined
  return undefined;
};
//选择器数组转化
export const optionsArray = (array, value, label, newAdd) => {
  if (newAdd) {
    if (array.includes(newAdd)) {
      array;
    } else {
      array.unshift(newAdd);
    }
  } else {
    array;
  }
  return array.map((item) => {
    return {
      value: item === newAdd ? "" : item[value] || item || "",
      label: item[label] || item,
    };
  });
};
//base64转化成下载链接
export const flieDownload = (base, name, type) => {
  const getFormat = (type) => {
    switch (type) {
      case "docx":
        return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
      case "xlsx":
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
      default:
        return "unknown"; // 增加一个默认值，以防传入未知类型
    }
  };
  if (typeof base === "object") return;
  const decodedString = atob(base);
  // 解码 Base64 字符串为二进制数据
  const byteNumbers = new Array(decodedString.length);
  for (let i = 0; i < decodedString.length; i++) {
    byteNumbers[i] = decodedString.charCodeAt(i);
  }
  const byteArray = new Uint8Array(byteNumbers);
  // 创建 Blob 对象
  const blob = new Blob([byteArray], { type: getFormat(type) });
  // 使用 URL.createObjectURL 创建一个临时 URL
  const url = URL.createObjectURL(blob);
  // 创建一个下载链接
  const link = document.createElement("a");
  link.href = url;
  link.download = name;
  link.click();
  // 释放 URL 对象
  URL.revokeObjectURL(url);
};
export const time = (time) => {
  const days = Math.floor(time / (24 * 3600));
  const hours = Math.floor((time % (24 * 3600)) / 3600);
  const minutes = Math.floor((time % 3600) / 60);
  const seconds = time % 60;
  const getTime = (val) => {
    return val.toString().padStart(2, "0");
  };

  // 判断是否全部为 0 并返回“未服务”
  if (days === 0 && hours === 0 && minutes === 0 && seconds === 0) {
    return `未服务`;
  }

  // 格式化输出
  return `${getTime(days)} 天 ${getTime(hours)} 小时 ${getTime(
    minutes
  )} 分钟 ${getTime(seconds)} 秒`;
};
//AES加密
export const encryptAES = (str) => {
  return CryptoJS.AES.encrypt(str, secretKey).toString();
};
//AES解密
export const decryptAES = (str) => {
  const bytes = CryptoJS.AES.decrypt(str, secretKey);
  return bytes.toString(CryptoJS.enc.Utf8);
};
